#include <iostream>
#include "comm.hPP"

int main()
{
    std::cout << "hello server!!" << std::endl;
    int c = CreateFifo(PIPE_PATH);
    assert(c);
    (void)c;

   sleep(20);
    std::cout<< "server begin"<<std::endl;
    int fd = open(PIPE_PATH, O_RDONLY); // server端口是读取数据
    //当写入端口还没打开的时候，server进程会阻塞在这里
    std::cout<< "server end"<<std::endl;
    if (fd < 0)
    {
        std::cout << "error" << strerror(errno) << std::endl;
        exit(1);
    }
    char buf[1024];
    while (true)
    {
        ssize_t s = read(fd, buf, sizeof(buf) - 1);
        //写端口写的慢或者不写，会阻塞在这里
        if (s > 0)
        { // 读取成功了
            buf[s]=0;//结尾加'\0'
            std::cout <<"clinet->>server# "<< buf << std::endl;
        }
        else if(s == 0){//写入端文件描述符关闭了
            std::cout << "写端关闭" << std::endl;
            break;
        }
        else{
            std::cout << "error：" << strerror(errno) << std::endl;
            break;
        }
    }
    close(fd);
    int r = RemoveFifo(PIPE_PATH);
    assert(r);
    (void)r;

    return 0;
}